package com.gome.axwl.aoc.common.util;

import com.cloudcache.client.cluster.base.CloudCacheClient;
import com.gome.common.serialization.FSTSerializationService;
import com.gome.common.serialization.SerializationService;
import com.gome.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

/**
 * 缓存工具类
 *
 * @author wangjie36@gome.com.cn
 * @date 2018/3/6
 * @copyright(c) gome inc Gome Co.,LTD
 */
@Component
public class CacheUtil<T> {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Redis
     */
    @Autowired
    private CloudCacheClient cloudCacheClient;

    /**
     * 序列化
     */
    private SerializationService serializationService;

    /**
     * 超时时间，秒
     */
    private int timeout = 1800;


    public CacheUtil() {
        serializationService = new FSTSerializationService();
    }

    public SerializationService getSerializationService() {
        return serializationService;
    }

    public void setSerializationService(SerializationService serializationService) {
        this.serializationService = serializationService;
    }

    public CloudCacheClient getRedisTemplate() {
        return cloudCacheClient;
    }

    public void setRedisTemplate(CloudCacheClient cloudCacheClient) {
        this.cloudCacheClient = cloudCacheClient;
    }


    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }


    public void hset(String key, String field, Object value) {
        logger.info("method[void hset(String key, String field, T value)] params:{key:{},field:{},value:{}}", new Object[]{key, field, value});
        byte[] buffer = SerializationUtils.serialize(value);
        Long result = cloudCacheClient.hset(key.getBytes(), field.getBytes(), buffer);
        System.out.println(result);
//        cloudCacheClient.expire(key, timeout);
    }

    public Object hget(String key, String field, Class<T> tClass) {
        logger.info("method[void get(String key, String field)] params:{key:{},field:{}}", new Object[]{key, field});
        if (StringUtil.isBlank(key) || StringUtil.isBlank(field)) {
            logger.error("method[void hget(String key, String field)] params error:{key:{},field:{}}", new Object[]{key, field});
        } else {
            final String value = cloudCacheClient.hget(key, field);
            System.out.println("hget value: " + value.getBytes());
            if (value != null) {
                return SerializationUtils.deserialize(value.getBytes());
            }
        }
        return null;
    }

    public void del(String key) {
        cloudCacheClient.del(key);
    }

    public void set(String key, Object value) {
        byte[] buffer = SerializationUtils.serialize(value);
        final String result = cloudCacheClient.set(key.getBytes(), buffer);
        cloudCacheClient.expire(key, timeout);
        logger.info("method[void set(String key, Object value)] params:{key:{},value:{}}, set result: {}", new Object[]{key, value, result});
    }

    public Object get(String key) {
        logger.info("method[void get(String key)] params:{key:{}}", new Object[]{key});
        if (StringUtil.isBlank(key)) {
            logger.error("method[void hget(String key)] params error:{key:{}}", new Object[]{key});
        } else {
            final byte[] value = cloudCacheClient.get(key.getBytes());
            System.out.println("hget value: " + value);
            if (value != null) {
                return SerializationUtils.deserialize(value);
            }
        }
        return null;
    }

    public boolean exists(String key) {
        if (StringUtil.isBlank(key)) {
            logger.error("method[void exist(String key)] params error:{key:{}}", new Object[]{key});
            return false;
        } else {
            return cloudCacheClient.exists(key.getBytes());
        }
    }

    public void incr(String key) {
        if (StringUtil.isBlank(key)) {
            logger.error("method[void incr(String key)] params error:{key:{}}", new Object[]{key});
        } else {
            cloudCacheClient.incr(key);
        }
    }
}

